//
//  SelectOne.m
//  ChampoinPeso
//
//  Created by mac on 2025/2/20.
//

#import "SelectOne.h"

@interface SelectOne()
@property (nonatomic,strong)UIView *pView;
@property  (nonatomic,strong)BRResultModel *model;
@end
@implementation SelectOne

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

-(void)setupUI
{
    UIView *view = [UIView new];
    [self addSubview:view];
    view.backgroundColor = UIColor.clearColor;
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_offset(0);
    }];
    UIImageView *bgIV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 454)];
    bgIV.image = [UIImage imageNamed:@"selectOneBg"];
    [self addSubview:bgIV];
    [self sendSubviewToBack:bgIV];
    UIButton *close = [UIButton buttonWithType:UIButtonTypeCustom];
    [close setImage:[UIImage imageNamed:@"selectClose"] forState:UIControlStateNormal];
    [self addSubview:close];
    [close mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(16);
        make.right.mas_offset(-16);
    }];
    
    [view addSubview:self.pView];
    UIButton *selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [selectBtn setImage:[UIImage imageNamed:@"selectTurn"] forState:UIControlStateNormal];
    [self addSubview:selectBtn];
    [selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(-bottomSafeHeight);
        make.left.mas_offset(23);
        make.right.mas_offset(-23);
        make.height.mas_equalTo(52);
    }];
    [close addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
    [selectBtn addTarget:self action:@selector(ccommit) forControlEvents:UIControlEventTouchUpInside];

}
-(void)close
{
    if (self.closeAction) {
        self.closeAction();
    }
}
-(void)ccommit
{
    if (self.commitAction) {
        self.commitAction(self.model);
    }
}
-(UIView *)pView
{
    if (!_pView) {
        _pView = [[UIView alloc]initWithFrame:CGRectMake(0, 80, SCREEN_WIDTH, 250)];
        _pView.backgroundColor = UIColor.clearColor;
    }
    return _pView;
}

-(void)setSource:(InfoModel *)model1 andkey:(int)index
{
    BRStringPickerView *stringPickerView = [[BRStringPickerView alloc]init];
    stringPickerView.backgroundColor = UIColor.clearColor;
    stringPickerView.pickerMode = BRStringPickerComponentSingle;
    NSMutableArray  *arr = [NSMutableArray array];
    NSArray *data = model1.delivered;
    BRResultModel *model;
    NSInteger vigorIndex = 0;
    NSString *str= CPS(model1.herin) ;
    if ([str isEqualToString:@""]) {
        model = [BRResultModel new];
        model.key = CPS(data[0][@"herin"]);
        model.value = CPS(data[0][@"madnevertheless"]);
        _model = model;
    }
    for (int i = 0; i < data.count; i++) {  
        NSMutableDictionary *dic = data[i];
        BRResultModel *model = [[BRResultModel alloc]init];
        model.key =CPS(dic[@"herin"]) ;
        model.value = CPS(dic[@"madnevertheless"]);
        [arr addObject:model];
        if ([CPS(model1.inquired)  isEqualToString:CPS(dic[@"madnevertheless"])]) {
            _model = model;
            vigorIndex = i;
        }
        if ([CPS(model1.herin)  isEqualToString:CPS(dic[@"herin"])]) {
            _model = model;
            vigorIndex = i;
        }

    }
    stringPickerView.dataSourceArr = arr;
    stringPickerView.selectIndex = vigorIndex;
    stringPickerView.numberOfComponents = 1;
    stringPickerView.changeModelBlock = ^(BRResultModel * _Nullable resultModel) {
        self->_model = resultModel;
    };
    BRPickerStyle *customStyle = [[BRPickerStyle alloc]init];
    customStyle.pickerColor = UIColor.clearColor;
    customStyle.pickerTextColor = UIColor.blackColor;
    customStyle.separatorColor = UIColor.clearColor;
    stringPickerView.pickerStyle = customStyle;
    [stringPickerView addPickerToView:self.pView];
}

@end
